package com.himiotech.core.project.web;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.himiotech.core.framework.config.HimioConfig;
import com.himiotech.core.framework.helper.utils.DateUtils;
import com.himiotech.core.framework.helper.utils.ServletUtils;
import com.himiotech.core.project.model.entity.Menu;
import com.himiotech.core.project.model.entity.Notice;
import com.himiotech.core.project.model.entity.user.User;
import com.himiotech.core.project.service.IConfigService;
import com.himiotech.core.project.service.IMenuService;
import com.himiotech.core.project.service.INoticeService;
import com.himiotech.core.project.web.base.HimioBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;

/**
 * @author wuwenbin
 **/
@Controller
public class IndexController extends HimioBaseController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IConfigService configService;
    @Autowired
    private HimioConfig himioConfig;
    @Autowired
    private INoticeService noticeService;

    @GetMapping("/")
    public String index(ModelMap mmp) {
        // 取身份信息
        User user = getSysUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUser(user);
        mmp.put("menus", menus);
        mmp.put("user", user);
        mmp.put("ignoreFooter", configService.selectConfigByKey("sys.index.ignoreFooter"));
        mmp.put("footerText", configService.selectConfigByKey("sys.index.footerText"));
        mmp.put("curVersion", himioConfig.getVersion());
        mmp.put("copyrightYear", himioConfig.getCopyrightYear());
        mmp.put("isDefaultModifyPwd", initPasswordIsModify(user.getPwdUpdateDate()));
        mmp.put("isPasswordExpired", passwordIsExpiration(user.getPwdUpdateDate()));
        mmp.put("latest5Notices", latest5Notice());

        // 菜单导航显示风格
        String menuStyle = configService.selectConfigByKey("sys.index.menuStyle");
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;
        return "topnav".equalsIgnoreCase(indexStyle) ? "indexTopNav" : "index";
    }

    /**
     * 默认首页
     *
     * @param mmp
     * @return
     */
    @GetMapping("/system/main")
    public String main(ModelMap mmp) {
        mmp.put("version", himioConfig.getVersion());
        return "main";
    }

    /**
     * 检查初始密码是否提醒修改
     *
     * @param pwdUpdateDate
     * @return
     */
    private boolean initPasswordIsModify(Date pwdUpdateDate) {
        Integer initPasswordModify = Convert.toInt(configService.selectConfigByKey("sys.account.initPasswordModify"));
        return initPasswordModify != null && initPasswordModify == 1 && pwdUpdateDate == null;
    }

    /**
     * 检查密码是否过期
     *
     * @param pwdUpdateDate
     * @return
     */
    private boolean passwordIsExpiration(Date pwdUpdateDate) {
        Integer passwordValidateDays = Convert.toInt(configService.selectConfigByKey("sys.account.passwordValidateDays"));
        if (passwordValidateDays != null && passwordValidateDays > 0) {
            if (pwdUpdateDate == null) {
                // 如果从未修改过初始密码，直接提醒过期
                return true;
            }
            Date nowDate = DateUtils.getNowDate();
            return DateUtils.differentDaysByMillisecond(nowDate, pwdUpdateDate) > passwordValidateDays;
        }
        return false;
    }

    private List<Notice> latest5Notice() {
        return noticeService.list(
                Wrappers.<Notice>query()
                        .eq("notice_type", 1)
                        .orderByDesc("gmt_create", "gmt_modified")
                        .last("limit 5"));
    }
}
